package com.hk.service.quartz;

import com.hk.commons.ConstantProduct;
import com.hk.commons.HqlFilter;
import com.hk.commons.StringHelper;
import com.hk.commons.entity.product.ProductInfo;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.entity.yingmi.YingmiConfirmFile;
import com.hk.service.product.IProductInfo;
import com.hk.service.sys.ISysUser;
import com.hk.service.yingmi.IYingmiConfirmFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class YimgmiUpdAccountIdQuartz extends BaseQuartz {
	private Logger logger = LoggerFactory.getLogger(YimgmiUpdAccountIdQuartz.class);

	@Autowired
	private IYingmiConfirmFile ymService;

	@Autowired
	private ISysUser userService;

	private void updUser() {
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addSql(" where 1=1 and t.accountId not in (select s.accountId from SysUser s where s.accountId is not null)  ");

		logger.info("select sql={}", hqlFilter.getWhereAndOrderHql());

		List<YingmiConfirmFile> orderList = ymService.findByFilter(hqlFilter);

		if(orderList == null || orderList.size() == 0){
			return  ;
		} else{
			logger.info("will update user account size  ={}", orderList.size());
		}

		for(YingmiConfirmFile order : orderList){
			String phoneNum = order.getBrokerUserId();
			SysUser user = userService.getByPhoneNum(phoneNum);
			if(user != null && StringHelper.isEmpty(user.getAccountId())){
				user.setAccountId(order.getAccountId());
				userService.update(user);
				logger.info("will update user account from null to ={}", order.getAccountId());
			}
		}
	}

	@Override
	public void start() {
		ExecutorService executor = Executors.newFixedThreadPool(1);
        Runnable task = new Runnable() {
            public void run() {
            	logger.info(" will start to upd user accountId by yingmi");
				updUser();
            }
        };
        executor.execute(task);
        executor.shutdown();
	}

}
